package com.base.vistter.iframe.inverter.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.base.vistter.iframe.bean.Result;
import com.base.vistter.iframe.bean.SystemContextHolder;
import com.base.vistter.iframe.entity.*;
import com.base.vistter.iframe.exception.IframeException;
import com.base.vistter.iframe.model.StationElectricityOverview;
import com.base.vistter.iframe.model.StationOverview;
import com.base.vistter.iframe.model.StationResponse;
import com.base.vistter.iframe.service.InverterStationService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/station")
public class InverterStationController {
    @Resource(name = "inverterStationService")
    private InverterStationService inverterStationService;

    /**
     * 分页加载电站信息
     */
    @GetMapping
    public Result<Page<StationResponse>> get(@RequestParam(value = "pageNo", defaultValue = "1") Integer pageNo, @RequestParam(value = "pageNum", defaultValue = "10") Integer pageNum, String name, Integer status, String field, String sort) throws IframeException {
        return Result.ok(inverterStationService.pageStationApp(pageNo, pageNum, name, status, field, sort));
    }

    @GetMapping("/collect")
    public Result<Page<StationResponse>> collect(@RequestParam(value = "pageNo", defaultValue = "1") Integer pageNo, @RequestParam(value = "pageNum", defaultValue = "10") Integer pageNum) throws IframeException {
        return Result.ok(inverterStationService.pageCollectStationApp(pageNo, pageNum));
    }

    @GetMapping("/{id}")
    public Result<InverterStation> getById(@PathVariable("id") Integer id) throws IframeException {
        return Result.ok(inverterStationService.getById(id));
    }

    /**
     * 保存电站
     *
     * @param inverterStation
     * @return
     * @throws IframeException
     */
    @PostMapping(produces = "application/json;charset=utf-8")
    public Result<Boolean> save(@RequestBody InverterStation inverterStation) throws IframeException {
        inverterStation.setSysUserId(SystemContextHolder.getSessionContext().getUserId());
        return Result.ok(inverterStationService.save(inverterStation));
    }

    /**
     * 电站概况
     */
    @GetMapping("/electricity")
    public Result<StationElectricityOverview> getStationElectricityOverview(Integer stationId) throws IframeException {
        return Result.ok(inverterStationService.getStationElectricityOverview(stationId));
    }

    /**
     * 我的电站列表
     */
    @GetMapping("my/station")
    public Result<List<InverterStation>> myStation() throws IframeException {
        return Result.ok(inverterStationService.myStation());
    }

    /**
     * 所有电站概况
     */
    @GetMapping("/overview")
    public Result<StationOverview> getStationOverview() throws IframeException {
        return Result.ok(inverterStationService.getStationOverview());
    }

    @GetMapping("/statistics/day")
    public Result<List<InverterDevicesPowerDay>> statisticsDay(@DateTimeFormat(pattern = "yyyy-MM-dd") Date time, Integer stationId) throws IframeException {
        return Result.ok(inverterStationService.statisticsDay(time, stationId));
    }

    @GetMapping("/statistics/month")
    public Result<List<InverterDevicesPowerMonth>> statisticsMonth(@DateTimeFormat(pattern = "yyyy-MM") Date time, Integer stationId) throws IframeException {
        if (time == null) {
            time = new Date();
        }
        return Result.ok(inverterStationService.statisticsMonth(time, stationId));
    }

    @GetMapping("/statistics/year")
    public Result<List<InverterDevicesPowerYear>> statisticsYear(Integer time, Integer stationId) throws IframeException {
        return Result.ok(inverterStationService.statisticsYear(time, stationId));
    }

    @GetMapping("/statistics/summary")
    public Result<List<InverterDevicesPowerSummary>> statisticsSummary(Integer stationId) throws IframeException {
        return Result.ok(inverterStationService.statisticsSummary(stationId));
    }
}
